﻿using System;
using System.Web;
using System.Threading;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Globalization;
namespace WebTeamplate
{
    public class BasePage : System.Web.UI.Page
    {
        protected Guid userloginId = Guid.NewGuid();
        protected string userLoginName = string.Empty;
        protected string applicationName = string.Empty;
        protected string languageId = string.Empty;
        private string masterPageName;
        private long pageID;
        // private SystemConfig sysConfig = new SystemConfig();
        protected override void InitializeCulture()
        {
            string culture = string.Empty;
            string languageFile = string.Empty;
            culture = "vi-VN";
            try
            {
                this.languageId = culture;
                this.Culture = culture;
                this.UICulture = culture;
            }
            catch
            {
                culture = "vi-VN";
                this.languageId = culture;
                this.Culture = culture;
                this.UICulture = culture;
               // (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture = culture;
            }
            this.SetCurrentCulture(culture);
            base.InitializeCulture();
        }

        void Application_Error(object sender, EventArgs e)
        {

        }
        protected override void OnPreInit(EventArgs e)
        {

            if (this.Page.AppRelativeVirtualPath.ToLower().Contains("~/admin/") || this.Page.AppRelativeVirtualPath.ToLower().Contains("~/recoverypassword.aspx"))
            {
                this.Theme = "Admin";
            }
            else
            {
                string url = this.GetCurrentPageUrl();
                if (url.ToLower().Trim() == "login.aspx")
                    this.Theme = "";
                else
                    this.Theme = GetSelectedThemes();

            }

            base.OnPreInit(e);
        }

        /// <summary>
        /// Get language that saved in ProfileCommon or seleced by event postback( dropdownlist, button, imagebutton,...).
        /// </summary>
        /// <returns>Return the selected theme, else return string.Empty.</returns>
        private string GetSelectedLanguage()
        {
            return "vi-VN";
        //    return (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
        }
        /// <summary>
        /// Get theme that saved in ProfileCommon or seleced by event postback( dropdownlist, button, imagebutton,...).
        /// </summary>
        /// <returns>Return the selected theme, else return string.Empty.</returns>
        private string GetSelectedThemes()
        {
            /*
            string id = Globals.ThemesSelectorID;
            string selectedTheme = string.Empty;
            if (id.Length > 0)
            {
                // if this is a postback caused by the theme selector's dropdownlist, retrieve
                // the selected theme and use it for the current page request
                if (this.Request.Form["__EVENTTARGET"] == id && !string.IsNullOrEmpty(this.Request.Form[id]))
                {
                    selectedTheme = this.Request.Form[id];
                    (HttpContext.Current.Profile as ProfileCommon).Preferences.Theme = selectedTheme;
                }
                else
                {
                    // if not a postback, or a postback caused by controls other then the theme selector,
                    // set the page's theme with the value found in the user's profile, if present
                    if (!string.IsNullOrEmpty((HttpContext.Current.Profile as ProfileCommon).Preferences.Theme))
                        selectedTheme = (HttpContext.Current.Profile as ProfileCommon).Preferences.Theme;
                }
            }
            else
            {
                selectedTheme = NgonNguUtil.Get(this.GetSelectedLanguage()).TenTheme;
            }
            return selectedTheme;*/
            return "Admin";
        }
        public void SetCurrentCulture(string culture)
        {
            CultureInfo objCul = new CultureInfo(culture, true);
            DateTimeFormatInfo objDateTimeFormatInfo = objCul.DateTimeFormat;
            objDateTimeFormatInfo.ShortDatePattern = "dd/MM/yyyy";
            objDateTimeFormatInfo.LongDatePattern = "dd/MM/yyyy";
            objDateTimeFormatInfo.DateSeparator = "/";
            objCul.DateTimeFormat = objDateTimeFormatInfo;

            NumberFormatInfo numFormat = objCul.NumberFormat;
            numFormat.NumberGroupSeparator = ".";
            numFormat.NumberDecimalSeparator = ",";
            numFormat.NumberDecimalDigits = 0;

            numFormat.CurrencyGroupSeparator = ".";
            numFormat.CurrencyDecimalDigits = 0;
            numFormat.CurrencyDecimalSeparator = ",";
            objCul.NumberFormat = numFormat;
            Thread.CurrentThread.CurrentCulture = objCul;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(culture);

        }
        public string GetCurrentPageName()
        {
            string s = GetCurrentPageUrl();
            // this.Request.ApplicationPath;
            string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
            string sRet = oInfo.Name;
            return sRet;
        }
        public string GetCurrentPageUrl()
        {
            string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            sPath = sPath.Substring(1, sPath.Length - 1);
            int index = sPath.IndexOf('/');
            while (index > 0)
            {

                sPath = sPath.Substring(index + 1, sPath.Length - (index + 1));
                index = sPath.IndexOf('/');

            }
            return sPath;
        }
        public string GetCurrentPageAndParamaterUrl()
        {
            string paramater = string.Empty;
            int indexP = System.Web.HttpContext.Current.Request.Url.ToString().IndexOf('?');
            string sPath = GetCurrentPageUrl();
            if (indexP >= 0)
            {
                paramater = System.Web.HttpContext.Current.Request.Url.ToString().Substring(indexP, System.Web.HttpContext.Current.Request.Url.ToString().Length - indexP);
                return sPath + paramater;
            }
            return sPath;
        }

        protected override void OnLoad(EventArgs e)
        {


            try
            {
                MembershipUser user = Membership.GetUser();
                this.applicationName = Membership.ApplicationName;
                if (user != null)
                {
                    userloginId = new Guid(user.ProviderUserKey.ToString());
                    userLoginName = user.UserName;
                }
                else userloginId = Guid.Empty;
            }
            catch
            { }

            base.OnLoad(e);

        }
        private bool IsOutSitePage()
        {
            /* if (this.Master != null)
             {
                 string master = this.Master.ToString();
                 int first = master.IndexOf('_');
                 int last = master.LastIndexOf('_');
                 string masterPageName = master.Substring(first + 1, last - first - 1);
                 string[] arr = SharedFunction.DEFAULT_OUTSITE_MASTERPAGE.Split('|');
                 if (arr.Length > 0)
                 {
                     for (int i = 0; i < arr.Length; i++)
                     {
                         if (arr[i].ToString().Trim().ToUpper() == masterPageName.ToUpper())
                             return true;
                     }
                 }
             }*/
            return false;
        }
        protected virtual void DoGet()
        {

        }
        protected virtual void DoPost()
        {

        }
        public string BaseUrl
        {
            get
            {
                string url = this.Request.ApplicationPath;
                if (url.EndsWith("/"))
                    return url;
                else
                    return url + "/";
            }
        }
        public string FullPageUrl
        {
            get
            {
                return this.Request.Url.AbsoluteUri;
            }
        }
        public string LanguageID
        {
            get
            {
                return this.languageId;
            }
        }
        public string FullBaseUrl
        {
            get
            {
                return this.Request.Url.AbsoluteUri.Replace(
                   this.Request.Url.PathAndQuery, "") + this.BaseUrl;
            }
        }
        public string ApplicationName
        {
            get
            {
                return this.applicationName;
            }
        }
        protected void RequestLogin()
        {
            this.Response.Redirect(FormsAuthentication.LoginUrl +
               "?ReturnUrl=" + this.Request.Url.PathAndQuery);
        }
        public Guid UserloginId
        {
            get
            {
                return this.userloginId;
            }
        }
        public string GetPageParams()
        {
            FilterFields Params = new FilterFields();
            return Params.ToString(FilterFieldType.All, "", ViewState);
        }
        public string GetPageParams(FilterFieldType preserve)
        {
            FilterFields Params = new FilterFields();
            return Params.ToString(preserve, "", ViewState);
        }
        public string GetPageParams(FilterFieldType preserve, string removeList)
        {
            FilterFields Params = new FilterFields();
            return Params.ToString(preserve, removeList, ViewState);
        }
        public string GetPageParamater()
        {
            string s=this.Request.RawUrl;
            int indexQ = s.IndexOf('?');
            if (indexQ < 0)
                return string.Empty;
            string l = s.Substring(0, indexQ);
            string r = s.Substring(indexQ + 1, s.Length - l.Length-1);
            return r;
        }
    }
}